<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-add" lay-event="edit">查看分段</a>
        </script>

    </div>
</div>
<script src="../../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        // 自定义数据加载函数
        function loadTableData(page, limit) {
            $.ajax({
                url: `/exams/${page}/${limit}`,
                type: 'GET',
                success: function(res) {
                    if (res.code === 200) {
                        // 重新渲染表格
                        table.reload('currentTableId', {
                            data: res.data.records,
                            page: {
                                curr: page,
                                limit: limit,
                                count: res.data.total
                            }
                        });
                    } else {
                        layer.msg("数据加载失败：" + res.msg);
                    }
                },
                error: function() {
                    layer.msg("请求失败，请检查网络或服务器");
                }
            });
        }

        // 表格渲染
        table.render({
            elem: '#currentTableId',
            data: [], // 初始为空数据
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'source', width: 120, title: '试卷名称', sort: true},
                {field: 'description', width: 120, title: '介绍'},
                {field: 'institute', width: 120, title: '所属学院', sort: true},
                {field: 'major', width: 120, title: '所属专业'},
                {field: 'grade', width: 120, title: '年级'},
                {field: 'examDate', width: 120, title: '考试日期'},
                {field: 'totalTime', width: 120, title: '持续时间'},
                {field: 'totalScore', width: 80, title: '总分'},
                {field: 'type', width: 130, title: '试卷类型'},
                {field: 'tips', width: 450, title: '考生提示'},
                {title: '操作', minWidth: 250, toolbar: '#currentTableBar', align: "center"}
            ]],
            page: {
                curr: 1,
                limit: 15,
                limits: [10, 15, 20, 50],
                layout: ['prev', 'page', 'next', 'limit', 'count'],
                groups: 5
            },
            skin: 'line'
        });

        // 初始加载数据
        loadTableData(1, 15);

        // 监听分页切换
        table.on('page(currentTableFilter)', function(obj) {
            console.log('分页切换：当前页', obj.curr, '每页条数', obj.limit);
            loadTableData(obj.curr, obj.limit);
        });


        /**
         * toolbar监听事件
         */
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑试卷',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'totalCharm.html?examCode=' + encodeURIComponent(data.examCode)
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
        });
    });

</script>

</body>
</html>